<?php

namespace app\myr\controller;
use app\wechat\service\JwtService;
use Decimal\Decimal;
use think\admin\Controller;
use think\Request;
use think\Db;
use think\Validate;
use app\myr\model\Jxsarea as JxsareaModel;
use app\myr\model\Area as AreaModel;
use app\myr\model\Jxs as JxsModel;
use think\admin\helper\QueryHelper;
class Xsarea extends Controller
{
    public $paren_id=24;//国代id
    public function index(){
       $jxs_code=input("id","");
        $parent=JxsModel::where("user_code",$jxs_code)->find();
        $parent["province"]=$this->get_area_value($parent["province"]);
        $parent["city"]=$this->get_area_value($parent["city"]);
        $this->assign("parents",$parent);
       JxsareaModel::mQuery()->layTable(function () {
            $id=input("id","");
            $this->assign("jxs_code",$id);
            $this->title = '销售范围|经销商编号：'.$id;
        }, static function (QueryHelper $query) {
            $uniacid=session("user.uniacid");
            $id=input("id","");
            $query->where("jxs_code",$id);
            $query->where("uniacid",$uniacid);
        });
    }
    
    
    protected function _index_page_filter(&$data)
    {
        
        foreach ($data as &$vo) {
            $vo["province"]=$this->get_area_value($vo["province"]);
            $vo["city"]=$this->get_area_value($vo["city"]);
            $vo["district"]=$this->get_area_value($vo["district"]);
        }
    }
    
    public function get_area_value($code=""){
        return AreaModel::where("area_code",$code)->value("name");
    }
    /**
     * 添加经销商
     */
    public function add(){
        $id=input("jxs_code","");
        $this->assign("jxs_code",$id);
        $paren=JxsModel::where("user_code",$id)->find();
        if($paren["paren_id"] != $this->paren_id){
            if($paren["role"] <= 3){ //省代
                $province=AreaModel::order("id desc")->where("area_code",$paren["province"])->select();
                $this->assign("province",$province);
            }else if($paren["role"] == 4){
                $province=AreaModel::order("id desc")->where("area_code",$paren["province"])->select();
                $this->assign("province",$province);
            }
            else{
                $province=AreaModel::order("id desc")->where("parent_code",0)->select();
                $this->assign("province",$province);
            }
        }else{
            $province=AreaModel::order("id desc")->where("parent_code",0)->select();
            $this->assign("province",$province);
        }
        JxsareaModel::mForm('form');
    }
    
    public function edit(){
        $province=AreaModel::order("id desc")->where("parent_code",0)->select();
        $this->assign("province",$province);
        JxsareaModel::mForm('form');
    }
    
    //设置经营范围
    protected function _form_filter(array &$vo)
    {
        $jxs_code=input("jxs_code","");
        $vo["jxs_code"]=$jxs_code;
        if ($this->request->isPost()) {
            $data=$this->request->post();
            if($this->request->action() == "add"){
				$uniacid=session("user.uniacid");
                if(empty($is)){
                    $vo["uniacid"]=$uniacid;
                }else{
                    $this->error("区域已存在");
                }
            }else if($this->request->action() == "edit"){
                unset($vo["jxs_code"]);
                //$where["username"]=$data["username"];
               // SystemUser::where($where)->update($data);
            }
        }
        else{
            if(isset($vo["province"])){
                if($vo["province"]){
                    $city=AreaModel::order("id desc")->where("parent_code",$vo["province"])->select();
                    $this->assign("city",$city);
                }
            }else{
                $vo["province"]="";
                $this->assign("city","");
            }
            
            if(isset($vo["city"])){
                if($vo["city"]){
                    $district=AreaModel::order("id desc")->where("parent_code",$vo["city"])->select();
                    $this->assign("district",$district);
                }
            }else{
                $vo["city"]="";
                $this->assign("district","");
            }
        }
    }
    
     //获取城市
    public function ajax_city(){
        if ($this->request->isGet()) {
            $data=$this->request->get();
            $jxs_code=$data["jxs_code"];
            if($data["city"]){
                $parent=JxsModel::where("user_code",$jxs_code)->find();
                if($parent["role"] == 1){ //省代
                    $pinvince=AreaModel::order("pinyin asc")->where("parent_code",$data["id"])->select();
                    echo json_encode($pinvince);die;
                }else if($parent["role"] == 2 || $parent["role"] == 3){
                    $pinvince=AreaModel::order("pinyin asc")->where("area_code",$parent["city"])->select();
                    echo json_encode($pinvince);die;
                }else{
                    $pinvince=AreaModel::order("pinyin asc")->where("parent_code",$data["id"])->select();
                    echo json_encode($pinvince);die;
                }
            }else{
                $pinvince=AreaModel::order("pinyin asc")->where("parent_code",$data["id"])->select();
                echo json_encode($pinvince);die;
            }
        }
    }
    
    //批量设置销售范围
    public function  setarea(){
        if ($this->request->isGet()) {
            $data=$this->request->get();
            $jxs_code=$data["jxs_code"];
            $parent=JxsModel::where("user_code",$jxs_code)->find();
            if($parent["role"] == 1){ //省代
                $where["jxs_code"]=$jxs_code;
                $province=AreaModel::where("parent_code",$parent["province"])->select();
                foreach($province as $kk=>$vv){
                    $dist=AreaModel::where("parent_code",$vv["area_code"])->select();
                    foreach ($dist as $jj=>$v){
                        $where["province"]=$parent["province"];
                        $where["city"]=$vv["area_code"];
                        $where["district"]=$v["area_code"];
                        $ids=JxsareaModel::where($where)->find();
                        if(empty($ids)){
                            JxsareaModel::insert($where);
                        }
                    }
                }
                echo "同步导入销售范围成功";die;
            }else if($parent["role"] > 1 && $parent["role"] < 4){
                $dist=AreaModel::where("parent_code",$parent["city"])->select();
                foreach ($dist as $jj=>$v){
                    $where["province"]=$parent["province"];
                    $where["city"]=$vv["area_code"];
                    $where["district"]=$v["area_code"];
                    $ids=JxsareaModel::where($where)->find();
                    if(empty($ids)){
                        JxsareaModel::insert($where);
                    }
                }
                echo "同步导入销售范围成功";die;
            }
        }
    }
    
    public function remove()
    {
		if ($this->request->isPost()) {
            $data=$this->request->post();
			$uu=JxsareaModel::where("id",$data["id"])->find();
			$where["id"]=$uu["id"];
			$is=JxsareaModel::where($where)->find();
			if($is){
				JxsareaModel::where($where)->delete();
			}
		}
        JxsareaModel::mDelete();
    }
}